package com.moredatasource.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/29  11:34
 * 描述:
 */
@Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages={"com.moredatasource.dao1"},
            transactionManagerRef = "transactionManager1",
            entityManagerFactoryRef = "entityMangerFactoryBean1"
    )
    public class JpaEntityMangerConfig1 {

    @Primary
    @Bean("entityMangerFactoryBean1")
    public LocalContainerEntityManagerFactoryBean getEntityMangerFactoryBean(
            EntityManagerFactoryBuilder entityManagerFactoryBuilder, @Qualifier("dataSource1")DataSource dataSource){

        return entityManagerFactoryBuilder.dataSource(dataSource)
                .packages("com.moredatasource.entity1")
                .build();
    }

    @Bean("transactionManager1")
    @Primary
    public PlatformTransactionManager getTransactionManager(@Qualifier("entityMangerFactoryBean1") EntityManagerFactory entityManagerFactory){
       return new JpaTransactionManager(entityManagerFactory);
    }

}
